package tsvetanv.inventory.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tsvetanv.inventory.dao.ProductDao;
import tsvetanv.inventory.model.Product;

/**
 * The <code>ProductServiceImpl</code> class implements the business logic
 * related with {@link Product} class.
 * 
 * @author Tsvetan Vasilev
 * 
 */

@Service("productService")
public class ProductServiceImpl implements ProductService {
	@Autowired
	private ProductDao dao;

	// --- Getters, Setters-----------------------------------------------------
	public void setDao(ProductDao dao) {
		this.dao = dao;
	}

	// --- Methods -------------------------------------------------------------

	public Product findByProductId(int productId) {
		return dao.findByProductId(productId);
	}

	public Product findByProductName(String productName) {
		return dao.findByProductName(productName);
	}

	public boolean isThereAnotherProductInDBWithSameName(Product product) {
		Product productInDatabase = dao.findByProductName(product.getName());

		// there is NOT any product with inputed name
		if (productInDatabase == null) {
			return false;
		}

		// there is a product with the inputed name

		if (product.getId() == productInDatabase.getId()) {
			// the edited Product is already in DB.
			return false;
		} else {
			// there is in DB a product which is different from edited product
			// but with the same name
			return true;
		}
	}

	// public void save(Product product) {
	// dao.save(product);
	//
	// }
	//
	// public void update(Product product) {
	// dao.update(product);
	//
	// }
	//
	// public void delete(Product product) {
	// dao.delete(product);
	//
	// }

}
